﻿using ASmile.Extend;
using ASmile.Helpers;
using ASmile.ORM.BuildSQLText;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASmile.Win
{
    public class BLLBase : WebApiClient
    {
        #region 静态成员

        static BLLBase()
        {
            ServerUrl = Config.Settings.ServerUrl;
            TokenName = Config.Settings.TokenName;
        }

        static string ServerUrl { get; }

        static string TokenName { get; }

        #endregion

        public BLLBase()
            : base(ServerUrl)
        { }

        protected override void RequestBefore()
        {
            SetHttpHeader(TokenName, TokenHelper.TokenValue);
        }

        protected override string BaseUrl
        {
            get
            {
                if (ControllerName.IsNotEmpty())
                {
                    return $"api/{ControllerName}";
                }
                return null;
            }
        }

        protected virtual string ControllerName { get; }

        protected override void RequestFailHandle(int statusCode, Exception exception)
        {
            System.Windows.Forms.MessageBox.Show($"请求失败，消息：{exception.Message}");
        }

        protected void SetToken(string token)
        {
            TokenHelper.SetToken(token);
        }
        /// <summary>
        /// 返回高级搜索的内容列表
        /// </summary>
        public List<AdSearchDetail> AdSearchList { get; } = new List<AdSearchDetail>();

        public string NewId()
        {
            return GuidHelper.NewId();
        }

        public void ClearADSql()
        {
            AdSearchList.Clear();
        }
        /// <summary>
        /// 获取服务器时间
        /// </summary>
        public DateTime GetServerTime()
        {
            var r = PostRequest<ApiResult<DateTime>>("api/ApiUser/GetServerTime", null);
            return r.data;
        }

        [WebApi("api/ASmile/KeepLive")]
        public ApiResult KeepLive()
        {
            return GetResult();
        }
    }
}